<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\Goods;
use app\model\Config;
use app\model\GoodsImage;
use app\model\GoodsDescription;
use app\model\GoodsDiscount;
use app\model\Category;
use app\model\Store;
use app\model\GoodsSku;
use app\model\GoodsSkuValue;
use app\model\MiaoshaGoods;
use app\model\TuanGoods;

use app\model\TuanFound;

class GoodsController extends Base
{
    public function index()
    {
        $weid = weid();
        $serach['keyword'] = input('post.keyword', '', 'serach_in');
        $serach['sid'] = input('post.sid', '', 'serach_in');
        $serach['ocid'] = input('post.ocid', '', 'serach_in');
        $serach['categoryId'] = input('post.categoryId');

        if (empty($serach['categoryId'])) {
            $serach['categoryId'] = input('get.categoryId');
        }

        $serach['is_recommended'] = input('post.is_recommended', '', 'serach_in');
        $serach['news'] = input('post.news');

        $priceOrder = input('post.priceOrder');
        $salesOrder = input('post.salesOrder');

        if (empty($serach['sid'])) {
            $serach['sid'] = $this->sid;
        }

        if (empty($serach['ocid'])) {
            $serach['ocid'] = $this->ocid;
        }

        if (arrayempty($serach)) {
            return $this->json([]);
        }

        $query = Goods::where(['weid' => $weid, 'status' => 1]);

        if (!empty($priceOrder)) {
            $Sort = 'price ' . $priceOrder;
        } elseif (!empty($salesOrder)) {
            $Sort = 'sale_count ' . $salesOrder;
        } else {
            $Sort = 'sort asc,id desc,is_recommended desc';
        }

        if (!empty($serach['sid'])) {
            $query->where('sid', $serach['sid']);
        }

        if (!empty($serach['ocid'])) {
            $query->where('ocid', $serach['ocid']);
        }

        if (!empty($serach['keyword'])) {
            $query->where('name', 'like', '%' . $serach['keyword'] . '%');
        }

        if (!empty($serach['news'])) {
            $query->where('is_new',  1);
        }

        if (!empty($serach['categoryId'])) {
            $categoryIds = Category::getsonid($serach['categoryId']);
            $query->where('cat_id', 'in',  $categoryIds);
        }

        if (!empty($serach['is_recommended']) || $serach['is_recommended'] === "0") {
            $query->where('is_recommended', $serach['is_recommended']);
        }

        $res = $query->order($Sort)
            ->paginate(getpage())
            ->toArray();

        foreach ($res['data'] as &$vo) {
            $vo['image'] = toimg($vo['image']);
            if (!empty($serach['sid'])) {
                if ($vo['sid'] == 0) {
                    $vo['name'] = '[自营]' . $vo['name'];
                } else {
                    $vo['name'] = '[' . Store::getTitle($vo['sid']) . ']' . $vo['name'];
                }
            }
            $vo['cat_id'] = Category::getTitle($vo['cat_id']);;
            $vo['create_time'] = time_format($vo['create_time']);
            $vo['update_time'] = time_format($vo['update_time']);
        }
        $res['data'] = Goods::setGoodslist($res['data']);

        $data['data'] = $res;

        return $this->json($data);
    }

    public function video()
    {
        $config = Config::getconfig();
        if (input('get.v') != $config['program_version']) {
            $weid = weid();
            $page = input('post.page', 1, 'intval');

            $query = Goods::where(['weid' => $weid, 'status' => 1]);

            $query->where('videotype', '>', 0);
            $Sort = 'sort asc,id desc';

            $res = $query->order($Sort)
                ->paginate(getpage())
                ->toArray();

            foreach ($res['data'] as &$vo) {
                $vo['image'] = toimg($vo['image']);
                $vo['videourl'] = toimg($vo['videourl']);

                $vo['images'] = GoodsImage::where(['goods_id' => $vo['id']])
                    ->order('id asc')
                    ->select()
                    ->toArray();

                foreach ($vo['images'] as &$imgvo) {
                    $imgvo['image'] = toimg($imgvo['image']);
                }

                $vo['create_time'] = time_format($vo['create_time']);
                $vo['update_time'] = time_format($vo['update_time']);
            }
            $res['data'] = Goods::setGoodslist($res['data']);

            $data['data'] = $res;
        }
        return $this->json($data);
    }

    public function indexgoods()
    {
        $is_recommended = input('get.is_recommended');
        $is_discount = input('get.is_discount');
        $is_new = input('get.is_new');
        $is_hot = input('get.is_hot');

        $where['weid'] = weid();
        $where['status'] = 1;
        if (!empty($is_recommended)) {
            $where['is_recommended'] = 1;
        }
        if (!empty($is_discount)) {
            $where['is_discount'] = 1;
        }
        if (!empty($is_new)) {
            $where['is_new'] = 1;
        }
        if (!empty($is_hot)) {
            $where['is_hot'] = 1;
        }

        $data = Goods::where($where)
            ->order('id desc')
            ->limit(4)
            ->select()
            ->toArray();

        $data = Goods::setGoodslist($data);
        return $this->json($data);
    }


    public function detail($id)
    {
        $msid = input('get.msid');
        $tuanid = input('get.tuanid');
        if (!empty($tuanid)) {
            $tuan = TuanGoods::find($tuanid);
            if (!empty($tuan)) {
                $tuan = $tuan->toArray();
                $tuan['TuanFound'] = TuanFound::where(['tuan_id' => $tuan['id'], 'weid' => weid(), 'status' => 0])->select()->toArray();
            }
        }

        if (!empty($msid)) {
            $miaosha = MiaoshaGoods::find($msid);
            if (!empty($miaosha)) {
                $miaosha = $miaosha->toArray();
            }
        }
        //增加点击
        Goods::where('id', $id)->inc('viewed')->update();

        $goodsdata = Goods::goodsInfo($id);
        if (!empty($tuan['price'])) {
            $goodsdata['original_price'] = $goodsdata['price'];
            $goodsdata['price'] = $tuan['price'];
        }
        if (!empty($miaosha['price'])) {
            $goodsdata['original_price'] = $goodsdata['price'];
            $goodsdata['price'] = $miaosha['price'];
        }

        $data = $goodsdata;
        $picdataarray = GoodsImage::where(['goods_id' => $id])
            ->order('id asc')
            ->select()
            ->toArray();

        foreach ($picdataarray as &$vo) {
            $vo['pic'] = toimg($vo['image']);
        }

        $data['videourl'] =  toimg($data['videourl']);

        $description = GoodsDescription::where(['goods_id' => $id])->find();

        if ($goodsdata['is_skumore'] == 1) {
            $data['skumore'] = GoodsSkuValue::get_goods_sku_value($id);
        } else {
            $data['attribute'] = GoodsSku::get_goods_sku($id);
        }

        if (!empty($goodsdata['cat_id'])) {

            $category = Category::find($goodsdata['cat_id']);
            if (!empty($category)) {
                $data['category'] = $category->toArray();
            }
        }
        if (empty($data['category']['deliverymode'])) {
            $data['category']['deliverymode'] = 3;
        }

        if ($data['quantity'] < 0) {
            $data['quantity'] = 0;
        }

        $data['minPrice'] = $goodsdata['price'];
        $data['pic'] = toimg($goodsdata['image']);
        $data['minPoints'] = $goodsdata['pay_points'];
        if ($goodsdata['ptype'] == 1) {
            $data['stores'] = $goodsdata['quantity'];
        } elseif ($goodsdata['ptype'] == 2) {
            $data['stores'] = 1;
        }

        $data['sale_count'] = $goodsdata['sale_count'] + $goodsdata['sale_count_base'];
        $data['viewed'] = $goodsdata['viewed'] + $goodsdata['viewed_base'];

        $goods_discount = GoodsDiscount::where(['goods_id' => $id])
            ->order('quantity ASC')
            ->select()
            ->toArray();;

        foreach ($goods_discount as &$vo) {
            $vo['price'] = round((intval($goodsdata['price'])) * percent_to_num($vo['price']) * 10, 2);
        }

        $data['goods_discount'] = $goods_discount;
        $data['tuan'] = $tuan;
        $data['miaosha'] = $miaosha;
        $data['price'] = $goodsdata['price'];
        $data['points'] = $goodsdata['pay_points'];
        $data['content'] = sethtmlimg($description->description);
        $data['pics'] = $picdataarray;
        return $this->json(['data' => $data]);
    }

    public function price()
    {

        $id = input('post.goodsId');
        $msid = input('post.msid');
        $tuanid = input('post.tuanid');
        $sku = input('post.sku');
        $goodsmob = new Goods;

        $goodsPrice = $goodsmob->cartGoods([
            'id' => $id,
            'sku' => $sku,
            'msid' => $msid,
            'tuanid' => $tuanid
        ]);

        $data['price'] = $goodsPrice['total'];
        $data['points'] = $goodsPrice['total_return_points'];

        if ($goodsPrice['ptype'] == 1) {
            $data['stores'] = $goodsPrice['stores'];
        } elseif ($goodsPrice['ptype'] == 2) {
            $data['stores'] = 1;
        }

        $data['image'] = $goodsPrice['image'];
        if ($goodsPrice['skuimage']) {
            $data['image'] = $goodsPrice['skuimage'];
        }
        return $this->json(['data' => $data]);
    }

    public function buynowinfo()
    {
        $goodsmob = new Goods;
        $id = input('post.goodsId');
        $msid = input('post.msid');
        $tuanid = input('post.tuanid');
        $jointuanid = input('post.jointuanid');

        $sku = input('post.sku');
        $is_skumore = input('post.is_skumore');
        $skumore = json_decode(input('post.skumore'), true);
        $buyNumber = input('post.buyNumber');

        $data = $goodsmob->cartGoods([
            'id' => $id,
            'sku' => $sku,
            'msid' => $msid,
            'tuanid' => $tuanid,
            'quantity' => $buyNumber,
            'is_skumore' => $is_skumore,
            'skumore' => $skumore
        ]);
        $data['is_combination'] = '0';
        $data['pic'] = toimg($data['image']);

        $data['goodslength'] = $data['length'];
        $data['jointuanid'] = $jointuanid;
        unset($data['length']);

        return $this->json(['data' => $data]);
    }

    public function kanjia()
    {
        return $this->json(['data' => $data]);
    }

    public function pingtuan()
    {
        return $this->json(['data' => $data]);
    }
    //返回推荐的商品
    public function recommend()
    {
        if (input('get.v') == Config::getconfig()['program_version']) {
            $data = [];
        } else {
            $where['weid'] = weid();
            $where['is_recommended'] = 1;
            $query = Goods::where($where);
            $query->where('sid', 0);
            $goodslist = $query->order('id desc')->limit(6)->select()->toArray();
            foreach ($goodslist as &$vo) {
                $vo['image'] = toimg($vo['image']);
            }
            $data = Goods::setGoodslist($goodslist);
        }
        return $this->json(['data' => $data]);
    }
}
